<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\common\Backend;
use app\common\model\Order;
use app\common\model\PublicStatus;
use think\db\Query;
use think\Request;

class OrderCon extends Backend
{
    protected $model = null;

    protected $quickSearchField = ['order_sn'];
    
    protected $withJoinTable = ['user'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new Order();
    }

     /**
     * 数据源
     * @throws Throwable
     */
    public function index(): void
    {

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit)->each(function($item,$key){
                $item['status_name'] = PublicStatus::$status[$item['status']];
                // $item['module_name'] = PublicStatus::$module[$item['module']];
                return $item;
            });

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => '',
        ]);
    }



    //订单统计
    public function info() : void {
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        //已支付订单数量
        $paid_order_quantity = $this->model->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)->where($where)->where(['order.status'=>PublicStatus::PUBLIC_STATUS_ONE])->count();

        //所有支付金额累加统计
        $actual_payment_amount = $this->model->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)->where($where)->where('order.status', PublicStatus::PUBLIC_STATUS_ONE)->sum('order.pay_price');
        //微信支付金额统计
        $weChat_payment_amount = $this->model->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)->where($where)->where('order.status', PublicStatus::PUBLIC_STATUS_ONE)->where('order.pay_type', PublicStatus::PAY_TYPE_ONE)->sum('pay_price');
        //支付宝支付金额统计
        $alipay_payment_amount = $this->model->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)->where($where)->where('order.status', PublicStatus::PUBLIC_STATUS_ONE)->where('order.pay_type', PublicStatus::PAY_TYPE_TWO)->sum('pay_price');
        $this->success('', [
            'actual_payment_amount'   => $actual_payment_amount, //实际支付金额
            'paid_order_quantity'  => $paid_order_quantity, //已支付订单数量
            'weChat_payment_amount'  => $weChat_payment_amount, //微信支付金额
            'alipay_payment_amount'  => $alipay_payment_amount, //支付宝支付金额
        ]);
    }


     //详情
     public function detail($id = null): void
     {
         $row = $this->model->where('id', $id)->find();
         
         if (!$row) {
             $this->error(('Record not found'));
         }
         if($row->user){
            $row['user'] =$row->user;
            $row['user_type_name'] = PublicStatus::$user_type[$row->user->user_type] ?? '未知用户';
         }
         $row['status_name'] = PublicStatus::$status[$row->status];
       
         $this->success('', [
             'row' => $row
         ]);
     }
}
